/**
 * 
 */
package com.effectiv.req1.controller;

import java.util.List;
import java.util.Locale;

import javax.validation.Valid;

import jodd.util.StringUtil;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.effectiv.req1.business.api.CompanyService;
import com.effectiv.req1.business.api.ProjectService;
import com.effectiv.req1.business.api.UserService;

import com.effectiv.req1.domain.Company;
import com.effectiv.req1.domain.Project;
import com.effectiv.req1.domain.User;

import com.effectiv.req1.repository.LovRepository;


/**
 * @author Dhrubo
 * 
 */
@Controller
@Slf4j
public class UserController {
	
	@Autowired
	private ProjectService projectService;

	@Deprecated
	@Autowired
	private LovRepository lovRepository;

	@Autowired
	private UserService userService;

	@Autowired
	private CompanyService companyService;

	@RequestMapping(value = "/listusers")
	public final String showHome(
			@RequestParam(value = "selection", required = false) boolean selection,
			@RequestParam(value = "projectId", required = false) String projectId,
			Model model) {
		model.addAttribute("users", userService.findUsersByCompany(null));
		model.addAttribute("selection", selection);
		Project project = null;

		if (StringUtil.isNotBlank(projectId)) {
			project = projectService.findOne(projectId);
			model.addAttribute(project);
		}
		return "listusers";
	}

	@RequestMapping(value = "/newuser")
	public final String newUserDisplay(Model model) {
		log.debug("Displaying UI to create new user");
		
		model.addAttribute(BeanUtils.instantiate(User.class));
		return "newuser";
	}

	@RequestMapping(value = "/registration")
	public final String showRegisterDisplay(Model model) {
		User user = BeanUtils.instantiate(User.class);
		user.setCompany(BeanUtils.instantiate(Company.class));
		model.addAttribute(user);
		return "registration";
	}

	@RequestMapping(value = "/registeruser", method = RequestMethod.POST)
	public @ResponseBody
	AjaxResponse register(@Valid User user, BindingResult result,
			AjaxResponse response, Locale locale) {
		if (!result.hasErrors()) {
			Company company = user.getCompany();
			companyService.save(company);

			user.setCompany(company);
			// save user
			userService.register(user, locale);
			response.setResult(user);
		} else {
			response.markValidationFailure(result.getFieldErrors());
		}

		return response;
	}

	@RequestMapping(value = "/activate")
	public final String activate(@RequestParam("user") String id, Model model,
			Locale locale) {
		User user = userService.activate(id, locale);
		
		
		
		model.addAttribute(user);

		return "activation";
	}

	@RequestMapping(value = "/saveuser", method = RequestMethod.POST)
	public @ResponseBody
	AjaxResponse saveUser(@Valid User user, BindingResult result,
			AjaxResponse response) {

		if (!result.hasErrors()) {
			userService.save(user);
			response.setResult(user);
		} else {

			response.markValidationFailure(result.getFieldErrors());
		}
		log.info("AjaxResponse = {}", response);
		return response;
	}

	@ExceptionHandler(DuplicateKeyException.class)
	@ResponseBody
	public AjaxResponse handleDuplicateKeyException(DuplicateKeyException ex) {

		AjaxResponse response = BeanUtils.instantiate(AjaxResponse.class);
		response.setStatus(AjaxResponse.ERROR);
		response.setMessage(ex.getLocalizedMessage());

		return response;

	}

	@RequestMapping(value = "/getcompanyusers")
	public @ResponseBody
	List<User> getCompanyUsers(@RequestParam("u") String user) {

		List<User> users = null;
		log.info("User - {}", user);

		if (StringUtil.isEmpty(user)) {
			users = userService.findUsersByCompany(null);
		} else {
			users = userService.findByCompanyAndNameLike(null, user);
		}

		log.info("users - {}", users);

		return users;
	}

}
